import 'dart:io';
import 'package:flutter/material.dart';
import 'package:my_app/src/rust/api/simple.dart';

class ShowDirWidget extends StatefulWidget {
  final String currentPath;

  const ShowDirWidget({super.key, required this.currentPath});

  @override
  State<ShowDirWidget> createState() => _ShowDirWidgetState();
}

class _ShowDirWidgetState extends State<ShowDirWidget> {
  List<String> file = [];

  String key = "";

  bool search = false;

  final TextEditingController _controller = TextEditingController();

  @override
  void initState() {
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    sadas().then((value) => null);

    return Scaffold(
        appBar: AppBar(
          title: const Text("杰微电子 - 孔祥旭Demo"),
          centerTitle: true,
          backgroundColor: Colors.lightBlue,
        ),
        body: Column(
          children: [
            Row(
              mainAxisAlignment: MainAxisAlignment.center,
              children: [
                Expanded(
                    child: TextField(
                  controller: _controller,
                  onChanged: (String text) {
                    if (text == "") {
                      setState(() {
                        search = false;
                        key = "";
                      });
                    } else {
                      setState(() {
                        search = true;
                        key = text;
                      });
                    }
                  },
                )),
                GestureDetector(
                  onTap: () {},
                  child: const Icon(Icons.search),
                )
              ],
            ),
            Expanded(
                child: ListView(
              children: search
                  ? searchPath()
                  : file.map((String filePath) => singleRow(filePath)).toList(),
            ))
          ],
        ));
  }

  @override
  void dispose() {
    _controller.dispose();
    super.dispose();
  }

  sadas() async {
    file.clear();

    Directory dir = Directory(widget.currentPath);

    // 获取目录下的所有实体
    List<FileSystemEntity> filesSync = dir.listSync();

    for (FileSystemEntity entity in filesSync) {
      if (entity is File && entity.path.endsWith(".jpg")) {
        setState(() {
          file.add(entity.path);
        });
      }
    }
  }

  List<Row> searchPath() {
    // 接收Rust处理之后的结果
    List<Row> list = [];

    // FFI调用, Rust语言提供的搜索函数
    List<String> tempList = searchFile(list: file, key: key);
    print(1111111);

    for (String path in tempList){
      list.add(singleRow(path));
    }

    // for (String path in file) {
    //   if (path.contains(key)) {
    //     list.add(singleRow(path));
    //   }
    // }


    return list;
  }

  Row singleRow(String filePath) {
    return Row(
      children: [
        Image.file(
          File(filePath),
          height: 50,
          width: 50,
        ),
        Expanded(
          child: Text(
            filePath.split("/")[6],
            maxLines: 1,
            overflow: TextOverflow.ellipsis,
            style: const TextStyle(fontSize: 10),
          ),
        )
      ],
    );
  }
}
